# -*- coding:utf-8 -*-
# File Name:port_util.py
# Author:jing
# Date:2018/12/18
import random

from model.mongo_model.resource_info import ResourceInfo

__author__ = 'jing'

PORT_POOL = {10250, 12379, 22379, 27017}


def get_possible_port():
    pool = set()
    for i in range(10000, 65534):
        pool.add(i)

    pool = pool.difference(PORT_POOL)
    for ri in ResourceInfo.query().get():  # type:ResourceInfo
        if ri.host_port in pool:
            pool.remove(ri.host_port)

    if pool:
        return list(pool)[random.randint(0, len(pool) - 1)]

    else:
        raise Exception
